package com.cyt.work.wb.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.work.wb.dao.WbBrandDao;
import com.cyt.work.wb.model.po.WbBrand;
import com.cyt.work.wb.model.vo.WbBrandVo;
import com.cyt.work.wb.service.WbBrandService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 维保模块-品牌表Service层实现类
 *
 * @author shengQiang Yu
 * @date 2019-11-28 9:12:41
 */
@Service("wbBrandService")
public class WbBrandServiceImpl extends BaseServiceImpl<WbBrand> implements WbBrandService {

    @Resource
    private WbBrandDao wbBrandDao;

    @Autowired
    public void setBaseDao(WbBrandDao wbBrandDao) {
        this.baseDao = wbBrandDao;
    }

    /**
     * 分页查询
     *
     * @param wbBrandVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:12:41
     */
    @Override
    public ReturnResult page(WbBrandVo wbBrandVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WbBrandVo> list = wbBrandDao.selectByCondition(wbBrandVo);
        PageInfo<WbBrandVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 根据ID获取数据
     *
     * @param id 主键ID
     * @return WbBrandVo
     * @author shengQiang Yu
     * @date 2019-11-28 9:12:41
     */
    @Override
    public WbBrandVo selectById(String id) {
        WbBrandVo wbBrandVo = new WbBrandVo();
        wbBrandVo.setId(id);
        List<WbBrandVo> wbBrandVoList = wbBrandDao.selectByCondition(wbBrandVo);
        if (wbBrandVoList != null && wbBrandVoList.size() == 1) {
            wbBrandVo = wbBrandVoList.get(0);
        }
        return wbBrandVo;
    }

    /**
     * 添加或更新
     *
     * @param wbBrandVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:12:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WbBrandVo wbBrandVo, SysUser sysUser) {
        if (StringUtils.isBlank(wbBrandVo.getId())) {
            WbBrand wbBrand = new WbBrand(sysUser);
            wbBrand.setApplyId(wbBrandVo.getApplyId());
            wbBrand.setName(wbBrandVo.getName());
            wbBrand.setBrandTypeId(wbBrandVo.getBrandTypeId());
            wbBrand.setBrandNum(wbBrandVo.getBrandNum());
            wbBrand.setProducter(wbBrandVo.getProducter());
            wbBrand.setProductAddress(wbBrandVo.getProductAddress());
            wbBrand.setRemarks(wbBrandVo.getRemarks());
            wbBrand.setSort(Integer.parseInt(wbBrandVo.getSort()));
            wbBrandDao.insert(wbBrand);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            WbBrand wbBrand = wbBrandDao.selectByPrimaryKey(wbBrandVo.getId());
            wbBrand.setApplyId(wbBrandVo.getApplyId());
            wbBrand.setName(wbBrandVo.getName());
            wbBrand.setBrandTypeId(wbBrandVo.getBrandTypeId());
            wbBrand.setBrandNum(wbBrandVo.getBrandNum());
            wbBrand.setProducter(wbBrandVo.getProducter());
            wbBrand.setProductAddress(wbBrandVo.getProductAddress());
            wbBrand.setRemarks(wbBrandVo.getRemarks());
            wbBrand.setSort(Integer.parseInt(wbBrandVo.getSort()));
            wbBrand.setUpdateTime(new Date());
            wbBrand.setUpdateUser(sysUser.getId());
            wbBrandDao.updateByPrimaryKey(wbBrand);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param wbBrandVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:12:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WbBrandVo wbBrandVo, SysUser sysUser) {
        //要修改的字段
        WbBrand wbBrand = new WbBrand();
        wbBrand.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        wbBrand.setUpdateUser(sysUser.getId());
        wbBrand.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WbBrand.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbBrandVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbBrandVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        wbBrandDao.updateByExampleSelective(wbBrand, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param wbBrandVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author shengQiang Yu
     * @date 2019-11-28 9:12:41
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WbBrandVo wbBrandVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WbBrand.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(wbBrandVo.getId())) {
            criteria.andIn("id", Arrays.asList(wbBrandVo.getId().split(",")));
        }
        wbBrandDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}